package cn.itcast.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.store.domain.Admin;
import cn.itcast.store.domain.User;
import cn.itcast.store.service.UserService;
import cn.itcast.store.service.serviceImp.UserServiceImp;
import cn.itcast.store.utils.MyBeanUtils;
import cn.itcast.store.utils.UUIDUtils;
import cn.itcast.store.web.base.BaseServlet;

public class UserServlet extends BaseServlet {

	// 注册页面跳转
	public String registUI(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		return "/jsp/register.jsp";
	}

	// 登录页面跳转
	public String loginUI1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		return "/jsp/login.jsp";
	}

	// 退出登录 ，清除session
	public String exitUI(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getSession().removeAttribute("loginUser");
		response.sendRedirect("index.jsp");
		return null;
	}

	// 注册
	public String regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Map<String, String[]> map =request.getParameterMap();
		User user01 = MyBeanUtils.populate(User.class, request.getParameterMap());

		// 2_部分数据是通过程序来设置的:uid,state,code
		user01.setUid(UUIDUtils.getId());
		user01.setState(0);
		user01.setCode(UUIDUtils.getId());
		System.out.println(user01);
		// 3_调用业务层注册功能,向用户邮箱发送一份激活邮件
		UserService UserService = new UserServiceImp();

		// 4_向客户端提示:用户注册成功,请激活,转发到提示页面
		try {
			UserService.userRegist(user01);
			request.setAttribute("msg", "用户注册成功,请激活!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", "用户注册失败");
			e.printStackTrace();
		}

		return "/jsp/info.jsp";

	}

	// 激活
	public String active(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String code = request.getParameter("code");

		UserService UserService = new UserServiceImp();

		User user = UserService.userActive(code);

		if (null != user) {
			user.setCode("");
			user.setState(1);
			UserService.updateUser(user);
			request.setAttribute("msg", "用户激活成功");
			return "/jsp/login.jsp";
		} else {
			request.setAttribute("msg", "用户激活失败,请重新注册");
			return "/jsp/register.jsp";
		}

	}

	// 登录跳转
	public String loginUI(HttpServletRequest request, HttpServletResponse response) {

		return "/jsp/login.jsp";

	}

	public String login(HttpServletRequest request, HttpServletResponse response) {
		// Map<String, String[]> map =request.getParameterMap();
		User user = MyBeanUtils.populate(User.class, request.getParameterMap());

		UserService UserService = new UserServiceImp();

		User user1;
		try {
			user1 = UserService.login(user);
			if (null != user1) {
				request.getSession().setAttribute("loginUser", user1);
				response.sendRedirect("index.jsp");
				return null;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//				System.out.println(user1.getName());
//				System.out.println(user1);
		request.setAttribute("msg", "用户登录失败，用户名和密码不匹配");
		return "/jsp/login.jsp";

	}

	public String Adminlogin(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("1111111111111111111111");
		// Map<String, String[]> map =request.getParameterMap();
		Admin admin = MyBeanUtils.populate(Admin.class, request.getParameterMap());

		UserService UserService = new UserServiceImp();

		Admin admin1;
		try {
			admin1 = UserService.Adminlogin(admin);
			if (null != admin1) {
				request.getSession().setAttribute("loginAdmin", admin1);
				request.getSession().setAttribute("name", admin.getUsername());
				response.sendRedirect("/shopping/admin/home.jsp");
				return null;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("msg", "用户登录失败，用户名和密码不匹配");
		return "/admin/index.jsp";

	}
}
